package com.whj.controller;
import com.whj.entity.Cart;
import com.whj.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @RequestMapping("/showCart")
    public String showCart(Model  model) {
        List<Cart> carts = cartService.showCart();
        model.addAttribute("carts", carts);
        return "cart";
    }
    @RequestMapping("/showCartByUserId")
    public String showCartByUserId(Integer userId, Model model) {
        List<Cart> carts = cartService.selectListById(userId);
        model.addAttribute("carts", carts);
        return "cart";
    }
    @RequestMapping("/addCart")
    public String addCart(Integer id ,Integer userId) {
        Cart cart=cartService.selectOne(id);
        System.out.println(cart);
        if(cart!=null){
            return "redirect:/cart/showCartByUserId?userId="+userId;
        }
        cartService.addCart(id, userId);
        return "redirect:/cart/showCartByUserId?userId="+userId;
    }
    @RequestMapping("/deleteCartById")
    public String deleteCart(Integer id, Integer userId) {
        cartService.deleteCartByid(id);
        return "redirect:/cart/showCartByUserId?userId="+userId;
    }
}
